package edu.lit.B180401;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import edu.lit.B180401.service.Service;

public class Show extends AppCompatActivity {
    Service service=new Service();



    private ListView hobbylist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        initView();

    }

    private void initView() {
        hobbylist = (ListView) findViewById(R.id.hobbylist);




        List<Map<String,Object>> list=getdata();

        SimpleAdapter simpleAdapter=new SimpleAdapter(this,list,
                R.layout.hobby,
                new String[]{"image","name","stuno","hobbys"},
                new int[]{R.id.imageView,R.id.name,R.id.stuno,R.id.hobbys1});
        hobbylist.setAdapter(simpleAdapter);
        hobbylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

    }


    public List<Map<String,Object>> getdata(){
        List<Student> stulist=service.selectStudent(this);
        List<Map<String,Object>> list=new ArrayList<>();
        for (int i=0;i<stulist.size();i++){
            Map<String,Object> map=new HashMap<>();
            List<HobbyInfo> hobbyinfolist=service.selectHobbtInfoByStuid(this,stulist.get(i).getId());
            String inter="";
            for(int j=0;j<hobbyinfolist.size();j++){
                System.out.println("error"+hobbyinfolist.get(j));
                inter+=service.selcetHobbyTypeById(this,hobbyinfolist.get(j).getHobbytypeid()).getName()+" ";
            }
            map.put("image",R.drawable.photo);
            map.put("name","姓名："+stulist.get(i).getName());
            map.put("stuno","学号："+stulist.get(i).getStuno());
            map.put("hobbys","爱好："+inter);
            list.add(map);
        }
        return  list;
    }
}
